package com.nuaa.community.service;

import com.nuaa.community.dao.DisscussPostMapper;
import com.nuaa.community.entity.DiscussPost;
import com.nuaa.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * ClassName: DiscussPostService
 * Package: com.nuaa.community.service
 * Description:
 */
@Service
public class DiscussPostService {

    @Autowired
    private DisscussPostMapper disscussPostMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    public List<DiscussPost> findDiscussPosts(int userId, int offset, int limit, int orderMode){
        return disscussPostMapper.selectDiscussPosts(userId, offset, limit, orderMode);
    }

    public int findDiscussPostRows(int userId){
        return disscussPostMapper.selectDiscussPostRows(userId);
    }

    /**
     * 插入帖子
     * @param post
     * @return
     */
    public int addDiscussPost(DiscussPost post){
        if (post == null){
            throw new IllegalArgumentException("参数不能为空！");
        }
        // 转义HTML标记
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));
        // 过滤敏感词
        post.setTitle(sensitiveFilter.filter(post.getTitle()));
        post.setContent(sensitiveFilter.filter(post.getContent()));
        // 插入帖子
        return disscussPostMapper.insertDiscussPost(post);
    }

    /**
     * 根据id查询帖子
     * @param id
     * @return
     */
    public DiscussPost findDiscussPostById(int id){
        return disscussPostMapper.selectDiscussPostById(id);
    }

    /**
     * 更新帖子评论
     * @param id
     * @param commentCount
     * @return
     */
    public int updateCommentCount(int id, int commentCount){
        return disscussPostMapper.updateCommentCount(id, commentCount);
    }

    public int updateType (int id, int type) {
        return disscussPostMapper.updateType(id, type);
    }

    public int updateStatus(int id, int status) {
        return disscussPostMapper.updateStatus(id, status);
    }

    /**
     * 更新帖子分数
     * @param id
     * @param score
     * @return
     */
    public int updateScore(int id, double score){
        return disscussPostMapper.updateScore(id, score);
    }
}
